import 'package:flutter/cupertino.dart';
import 'package:enterprise_v8/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v8/app/core/core.dart';
import 'package:enterprise_v8/app/core/ext/assets.dart';
import 'package:enterprise_v8/app/core/ext/widget_extension.dart';
import 'package:enterprise_v8/app/core/utils/user_manager.dart';
import 'package:enterprise_v8/app/data/entities/auth/function_entity.dart';
import 'package:enterprise_v8/generated/assets.gen.dart';
import 'package:enterprise_v8/router/app_pages.dart';
import 'user_control.dart';

/// description: 用户模块
class UserPage extends StatelessWidget {
  UserPage({super.key});

  final control = Get.put(UserControl());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              color: Color(0xffF9FBFF),
            ),
          ),
          Column(
            children: [
              SizedBox(height: MediaQuery.of(context).padding.top),
              Expanded(
                child: SingleChildScrollView(
                  child: Container(
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    child: Column(
                      children: [
                        AppGaps.vGap20,
                        // 基本信息
                        _getUserInfo(),
                        // 功能信息
                        _getFunctions(),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  /// 白色圆角背景
  Decoration getWhiteRoundedBg() {
    return BoxDecoration(
      borderRadius: BorderRadius.circular(10.w),
      color: Colors.white,
      boxShadow: [
        BoxShadow(
          color: const Color(0x140545C7),
          offset: Offset(0, 3.w),
          blurRadius: 4.w,
        ),
      ],
    );
  }

  /// 基本信息
  Widget _getUserInfo() {
    return Stack(
      children: [
        Container(
          height: 100.w,
          margin: EdgeInsets.only(top: 20.w),
          decoration: getWhiteRoundedBg(),
        ),
        GetBuilder(
          init: control,
          builder: (_) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 20.w),
                      width: 77.w,
                      height: 77.w,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.w),
                        color: Colors.white,
                        border: Border.all(color: Colors.white, width: 2.w),
                      ),
                      child: 'https://scpic.chinaz.net/files/pic/pic9/202207/apic42382.jpg'.asImg(
                        clickShowLarge: true,
                        fit: BoxFit.cover,
                        radius: 7.w,
                      ),
                    ),
                    AppGaps.hGap14,
                    // 用户名称
                    Expanded(
                      child: Text(
                        UserManager.getUser()?.systemUser?.nickname ?? '',
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 18.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ).marginOnly(top: 20.w),
                    ),
                    // 扫描按钮
                    IconButton(
                      icon: const Icon(
                        CupertinoIcons.qrcode_viewfinder,
                        color: Colors.black,
                      ),
                      onPressed: control.onTapScan,
                    ).marginOnly(top: 20.w),
                    Container(
                      margin: EdgeInsets.only(right: 10.w),
                      padding: EdgeInsets.only(left: 5.w, top: 2.w, right: 5.w, bottom: 2.w),
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(4.w),
                        color: Colors.white,
                        border: Border.all(
                          color: const Color(0xFFEFF2F6),
                          width: 1,
                        ),
                      ),
                      child: Row(
                        children: [
                          Assets.svg.iconMyEdit.svg(width: 12.w),
                          AppGaps.hGap2,
                          Text(
                            '编辑资料',
                            style: TextStyle(
                              color: const Color(0xFF757579),
                              fontSize: 12.sp,
                              fontWeight: FontWeight.w400,
                            ),
                          ),
                        ],
                      ),
                    ).marginOnly(top: 20.w).clickOnce(() {
                      Get.toNamed(Routes.myInfo);
                    })
                  ],
                ),
                Container(
                  alignment: Alignment.topLeft,
                  padding: EdgeInsets.only(left: 20.w, right: 12.w, top: 16.w, bottom: 20.w),
                  constraints: BoxConstraints(
                    minHeight: 60.w,
                  ),
                  decoration: getWhiteRoundedBg(),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      // 公司名称
                      Text(
                        '四川省美丰化肥科技有限公司',
                        style: TextStyle(
                          color: const Color(0xFF757579),
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                      AppGaps.vGap10,
                      // 职务
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 1.w),
                        decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(4.w),
                            border: Border.all(
                              color: const Color(0xFF3381F2),
                              width: 0.5.w, // 边框线宽度
                            )),
                        child: Text(
                          '管理员',
                          style: TextStyle(
                            color: const Color(0xFF3381F2),
                            fontSize: 12.sp,
                            fontWeight: FontWeight.w400,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
              ],
            );
          },
        )
      ],
    );
  }

  /// 单个功能样式
  Widget _getFunctionItem({required String title, required String icon}) {
    return SizedBox(
      height: 40.w,
      child: Row(
        children: [
          SizedBox(width: 24.w, height: 24.w, child: icon.isEmpty ? const Text('') : icon.asImg()),
          AppGaps.hGap20,
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                color: Colors.black,
                fontSize: 16.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
          Icon(Icons.arrow_forward_ios, size: 16.sp),
        ],
      ),
    );
  }

  /// 功能列表样式
  Widget _getFunctionList(List<FunctionEntity> functionList) {
    return Container(
      margin: EdgeInsets.only(top: 20.w),
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.w),
      decoration: getWhiteRoundedBg(),
      child: ListView.separated(
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: functionList.length,
        separatorBuilder: (context, index) {
          return const CustomGradientLine().marginOnly(left: 40.w, top: 10.w, bottom: 10.w);
        },
        itemBuilder: (context, index) {
          return _getFunctionItem(
            title: functionList[index].title,
            icon: functionList[index].icon,
          ).clickOnce(() {
            functionList[index].onTap();
          });
        },
      ),
    );
  }

  /// 功能信息
  Widget _getFunctions() {
    return Column(
      children: [
        _getFunctionList([
          FunctionEntity(title: '版本信息', icon: Assets.svg.iconVersionInfo.path, onTap: control.onTapVersion),
          FunctionEntity(title: '客服与帮助', icon: Assets.svg.iconHelp.path, onTap: control.onTapHelp),
          FunctionEntity(title: '设置', icon: Assets.svg.iconSetting.path, onTap: control.onTapSetting)
        ]),
        _getFunctionList([
          FunctionEntity(title: '切换用户', icon: Assets.svg.iconSwitchUser.path, onTap: control.onTapSwitchUser),
          FunctionEntity(title: '退出登录', icon: Assets.svg.iconExit.path, onTap: UserManager.logout),
        ]),
      ],
    );
  }
}
